package com.retailshop.shopuser.common;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.retailshop.shopuser.entity.UserNo;
import org.springframework.context.annotation.Configuration;

import java.util.Calendar;
import java.util.Date;

@Configuration
public class TokenUtils {

    public String getToken(UserNo userNo) {
        Date date = new Date();
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.HOUR, 150);
        Date expiresTime = nowTime.getTime();
        String token = "";

        String str = "xlsyear2019";

        token = JWT.create()
                .withAudience(userNo.getId() + "")
                .withExpiresAt(expiresTime)
                .withIssuedAt(date)
                .sign(Algorithm.HMAC256(str));
        return token;
    }

    public static int getUserNoId(String token) {
        int userNoId = 0;
        String userNoIds = null;
        try {
            userNoIds = JWT.decode(token).getAudience().get(0);
        } catch (JWTDecodeException e) {
            return 0;
        }
        if (StringUtil.isNumeric(userNoIds)) {
            userNoId = Integer.parseInt(userNoIds);
        }
        return userNoId;
    }




    public static void main(String[] args) {
        System.out.println("");
    }
}










